package com.tang4j.model.factory;

import com.tang4j.core.model.easyui.MenuBar;
import com.tang4j.core.model.easyui.SideMenu;
import com.tang4j.core.model.easyui.TreeGrid;
import com.tang4j.core.util.InstanceUtil;
import com.tang4j.model.ThSysNavigation;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

public class NavFactory {

    /**
     * sideMenu
     *
     * @param nav
     * @return
     */
    public static SideMenu createEasyUISideMenu(@NotNull ThSysNavigation nav) {

        return (SideMenu) new SideMenu()
                .setCode(nav.getCode())
                .setTag(nav.getRequestUrl())
                .setParentId(nav.getPcode())
                .setPcode(nav.getPcode())
                .setText(nav.getName())
                .setId(nav.getId())
                .setIconCls(nav.getIconCls());
    }

    /**
     * sideMenu
     *
     * @param navList
     * @return
     */
    public static List<SideMenu> createEasyUISideMenuList(@NotEmpty List<ThSysNavigation> navList) {
        List<SideMenu> menuList = new ArrayList<>();
        navList.forEach(nav -> menuList
                .add(NavFactory.createEasyUISideMenu(nav)));
        return menuList;
    }

    /**
     * MenuBar
     *
     * @param nav
     * @return
     */
    public static MenuBar createEasyUIMenuBar(@NotNull ThSysNavigation nav) {

        return (MenuBar) new MenuBar()
                .setCode(nav.getCode())
                .setParentId(nav.getPcode())
                .setPcode(nav.getPcode())
                .setText(nav.getName())
                .setId(nav.getId())
                .setUrl(nav.getRequestUrl())
                .setIconCls(nav.getIconCls());
    }

    /**
     * MenuBar
     *
     * @param navList
     * @return
     */
    public static List<MenuBar> createEasyUIMenuBarList(@NotEmpty List<ThSysNavigation> navList) {
        List<MenuBar> menuList = new ArrayList<>();
        navList.forEach(nav -> menuList
                .add(NavFactory.createEasyUIMenuBar(nav)));
        return menuList;
    }


    public static TreeGrid createTree(@NotNull ThSysNavigation nav) {
        return new TreeGrid(String.valueOf(nav.getId()), nav.getCode(), nav.getPcode(), nav.getName(),
                nav.getIconCls(), true, nav.getState() != null ?
                (String.valueOf(nav.getState()).equals("0") ? "closed" : "open") : "closed");
    }

    public static List<TreeGrid> createTree(@NotEmpty List<ThSysNavigation> navList) {
        List<TreeGrid> treeGrids = InstanceUtil.newArrayList();
        navList.forEach(nav -> treeGrids.add(NavFactory.createTree(nav)));
        return treeGrids;
    }


}
